<?php
/**
 * ----------------------------------------------------------
 * date: 2020/7/21 15:24
 * ----------------------------------------------------------
 * author: Raoxiaoya
 * ----------------------------------------------------------
 * describe:
 * ----------------------------------------------------------
 */

declare(strict_types=1);

namespace App\Utils;


class Json
{
    public const SUCCESS = 1;
    public const ERROR = 0;
    public const NORMAL = 2;

    protected static $levels = [
        'SUCCESS' => self::SUCCESS,
        'ERROR'   => self::ERROR,
        'NORMAL'  => self::NORMAL
    ];

    private static function getReturnStruct($levels):array{
        $arr = [];
        switch ( SELF::$levels[$levels]){
            case self::SUCCESS:
                $arr['code'] = self::SUCCESS;
                $arr['message'] = '';
                $arr['data'] = [];
            break;
            case self::ERROR:
                $arr['code'] = self::ERROR;
                $arr['message'] = '';
            break;
            case self::NORMAL:
                $arr['code'] = self::NORMAL;
                $arr['message'] = '';
            break;
        }
        return $arr;
    }

    public static function returnJson(string $levels,string $message, array $data = [])
    {
        $struct = self::getReturnStruct($levels);
        $struct['message'] = $message;
        if (isset($struct['data'])){
            $struct['data'] = $data;
        }
        return json_encode($struct, JSON_UNESCAPED_UNICODE);
    }

    public static function returnArray(string $levels,string $message, array $data = [])
    {
        $struct = self::getReturnStruct($levels);
        $struct['message'] = $message;
        if (isset($struct['data'])){
            $struct['data'] = $data;
        }
        return $struct;
    }
}